package cc.comac.mom.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cc.comac.mom.api.EquipSparePartsTypeService;
import cc.comac.mom.bean.EquipSparePartsType;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.util.ExportExcel;
import cc.comac.mom.util.ResultBean;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/EquipSparePartsType")
public class EquipSparePartsTypeController {

	@Autowired
	private EquipSparePartsTypeService equipSpareSartsTypeService;

	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public ResultBean save(@RequestBody EquipSparePartsType obj) {

		return equipSpareSartsTypeService.save(obj);

	}

	@RequestMapping(value = "/update",method = RequestMethod.POST)
	public ResultBean update(@RequestBody EquipSparePartsType obj) {

		return equipSpareSartsTypeService.update(obj);

	}

	@RequestMapping(value = "/deleteBatch",method = RequestMethod.POST)
	public ResultBean deleteBatch(@RequestBody List<EquipSparePartsType> list) {

		return equipSpareSartsTypeService.deleteBatch(list);

	}

	@ApiOperation(value = "获取所有启用的备件类型")
	@RequestMapping(value = "/getAllEnable",method = RequestMethod.POST)
	public ResultBean getAllEnable() {
		return equipSpareSartsTypeService.getAllEnable();
	}

	@RequestMapping(value = "/getById",method = RequestMethod.POST)
	public ResultBean getById(@RequestBody EquipSparePartsType obj) {
		ResultBean resultBean = new ResultBean(true);
		EquipSparePartsType equipSpareSartsType = equipSpareSartsTypeService.getById(obj);
		resultBean.setObject(equipSpareSartsType);
		return resultBean;

	}

	@RequestMapping(value = "/selectByCondition",method = RequestMethod.POST)
	public ResultBean selectByCondition(@RequestBody EquipSparePartsType obj) {
		ResultBean resultBean = new ResultBean(true);
		ResultDev resultDev = equipSpareSartsTypeService.selectByCondition(obj);
		resultBean.setObject(resultDev);
		return resultBean;

	}

	// 备件类型模板下载
	@RequestMapping(value = "EquipSparePartsType/TemplateDownload")
	public @ResponseBody void TemplateDownload(HttpServletResponse request, HttpServletResponse response)
			throws Exception {
		String fileName = "备件类型" + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls";
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("sheet1");
		// HSSFCellStyle cellStyle = workbook.createCellStyle();
		//// 设置单元格水平方向对齐方式 居中
		// cellStyle.setAlignment(HorizontalAlignment.LEFT);
		HSSFCellStyle cellStyle2 = workbook.createCellStyle();
		// 设置字体样式
		HSSFFont font = workbook.createFont();
		font.setFontName("微软雅黑");
		// 设置字号
		font.setFontHeightInPoints((short) 12);
		cellStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		cellStyle2.setFillForegroundColor(IndexedColors.YELLOW.index);
		// 字体风格放入
		cellStyle2.setFont(font);

		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);

		HSSFRow filedsRow = sheet.createRow(0);

		HSSFCell cell0 = filedsRow.createCell(0);
		cell0.setCellStyle(cellStyle2);
		HSSFRichTextString text0 = new HSSFRichTextString("类型编码(唯一性)");
		cell0.setCellValue(text0);

		HSSFCell cell1 = filedsRow.createCell(1);
		cell1.setCellStyle(cellStyle2);
		HSSFRichTextString text1 = new HSSFRichTextString("类型名称(唯一性)");
		cell1.setCellValue(text1);

		HSSFCell cell2 = filedsRow.createCell(2);
		cell2.setCellStyle(cellStyle2);
		HSSFRichTextString text2 = new HSSFRichTextString("类型备注");
		cell2.setCellValue(text2);

		HSSFCell cell3 = filedsRow.createCell(3);
		cell3.setCellStyle(cellStyle2);
		HSSFRichTextString text3 = new HSSFRichTextString("是否启用");
		cell3.setCellValue(text3);

		// 清空response
		response.reset();
		// 设置response的Header
		// 告诉浏览器用什么软件可以打开此文件
		response.setContentType("application/octet-stream");
		response.addHeader("Content-Disposition",
				"attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1"));
		OutputStream out = response.getOutputStream();
		workbook.write(out);
		workbook.close();
		out.flush();
		out.close();
	}

	// 备件类型导入数据
	@RequestMapping(value = "/EquipSparePartsType/TemplateDataUpload")
	public ResultBean dataUpload(@RequestPart("file") MultipartFile file, HttpServletRequest request)
			throws IOException {
		// 文件所在的临时文件夹
		String rootPath = request.getServletContext().getRealPath("/") + "uploadExcel";
		ResultBean resultBean2 = equipSpareSartsTypeService.importSparePartsType(file, rootPath);
		return resultBean2;
	}

	@ApiOperation(value = "导出数据")
	@RequestMapping("toExportExcel")
	public void exportExcel(HttpServletRequest request, HttpServletResponse response,
			@RequestBody EquipSparePartsType obj) throws IOException {
		String filePath = equipSpareSartsTypeService.exportData(request, response, obj);

		ExportExcel.download(filePath, response);
		ExportExcel.createDir(filePath);

	}


}
